{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ba1e88eb44785e80",
   "metadata": {},
   "source": [
    "# Prompts\n",
    "> A prompt for a language model is a set of instructions or input provided by a user to guide the model's response, helping it understand the context and generate relevant and coherent language-based output, such as answering questions, completing sentences, or engaging in a conversation.<br>\n",
    "> \n",
    "> 语言模型的提示是用户提供的一组指令或输入，用于指导模型的响应，帮助它理解上下文并生成相关且连贯的基于语言的输出，例如回答问题、完成句子或参与对话"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ca8bb6bec820c7b",
   "metadata": {},
   "source": [
    "## PromptTemplate\n",
    "> Use PromptTemplate to create a template for a string prompt.<br>\n",
    "> By default, PromptTemplate uses Python's str.format syntax for templating.<br>\n",
    "> \n",
    "> 用于 PromptTemplate 为字符串提示创建模板。<br>\n",
    "> 默认情况下， PromptTemplate 使用 Python 的 str.format 语法进行模板化。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98f5e3aade090827",
   "metadata": {},
   "source": [
    "### Instantiation\n",
    "> BasePromptTemplate --> StringPromptTemplate --> PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6e2886d69dfe256e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T06:51:18.628952Z",
     "start_time": "2024-07-17T06:51:18.622953Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PromptTemplate(input_variables=['adjective', 'content'], template='Tell me a {adjective} joke about {content}.')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "# Instantiation using from_template (recommended)\n",
    "prompt = PromptTemplate.from_template(\n",
    "    \"Tell me a {adjective} joke about {content}.\"\n",
    ")\n",
    "prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "81d532ff5cb0b4b6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T06:52:51.954683Z",
     "start_time": "2024-07-17T06:52:51.949403Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PromptTemplate(input_variables=['adjective', 'content'], template='Tell me a {adjective} joke about {content}.')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Instantiation using initializer\n",
    "PromptTemplate(\n",
    "    input_variables=[\"adjective\",\"content\"], \n",
    "    template=\"Tell me a {adjective} joke about {content}.\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea6cd76ed5045cbe",
   "metadata": {},
   "source": [
    "### Format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "70a418257b89d8aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T07:43:38.440658Z",
     "start_time": "2024-07-17T07:43:38.435297Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tell me a funny joke about chickens.'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call PromptTemplate.format()\n",
    "prompt.format(adjective=\"funny\", content=\"chickens\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8b308c0dfde7c71b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T06:26:30.804647Z",
     "start_time": "2024-07-17T06:26:30.800009Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StringPromptValue(text='Tell me a funny joke about chickens.')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call StringPromptTemplate.format_prompt()\n",
    "prompt.format_prompt(adjective=\"funny\", content=\"chickens\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8a1b9a7dce6f9705",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T06:26:30.815562Z",
     "start_time": "2024-07-17T06:26:30.805658Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StringPromptValue(text='Tell me a funny joke about chickens.')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call BasePromptTemplate.invoke()\n",
    "prompt.invoke(\n",
    "    {\n",
    "        \"adjective\":\"funny\", \n",
    "        \"content\":\"chickens\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ae5fd84b6fed738",
   "metadata": {},
   "source": [
    "## ChatPromptTemplate\n",
    "> The prompt to chat models/ is a list of chat messages.<br>\n",
    "> Each chat message is associated with content, and an additional parameter called role. For example, in the OpenAI Chat Completions API, a chat message can be associated with an AI assistant, a human or a system role.<br>\n",
    "> \n",
    "> 聊天模型/的提示是聊天消息列表。<br>\n",
    "> 每条聊天消息都与内容相关联，还有一个名为 role 的附加参数。例如，在 OpenAI 聊天完成 API 中，聊天消息可以与 AI 助手、人类或系统角色相关联。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6544638241eafb49",
   "metadata": {},
   "source": [
    "### Instantiation\n",
    "> BasePromptTemplate --> BaseChatPromptTemplate --> ChatPromptTemplate\n",
    "> \n",
    "> BaseMessagePromptTemplate \n",
    "> --> _StringImageMessagePromptTemplate \n",
    "> --> SystemMessagePromptTemplate、AIMessagePromptTemplate、 HumanMessagePromptTemplate\n",
    "> \n",
    "> BaseMessage --> SystemMessage、AIMessage、HumanMessage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "30f594f6c72b5130",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptTemplate(input_variables=['name', 'user_input'], messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=['name'], template='You are a helpful AI bot. Your name is {name}.')), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='Hello, how are you doing?')), AIMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template=\"I'm doing well, thanks!\")), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['user_input'], template='{user_input}'))])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "# Instantiation using from_messages (recommended)\n",
    "chat_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", \"You are a helpful AI bot. Your name is {name}.\"),\n",
    "        (\"human\", \"Hello, how are you doing?\"),\n",
    "        (\"ai\", \"I'm doing well, thanks!\"),\n",
    "        (\"human\", \"{user_input}\"),\n",
    "    ]\n",
    ")\n",
    "chat_template"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ff877eaec177c1d9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T07:44:53.367951Z",
     "start_time": "2024-07-17T07:44:53.361201Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptTemplate(input_variables=[], messages=[SystemMessage(content='You are a helpful AI bot. Your name is {name}.'), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='Hello, how are you doing?'))])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import SystemMessage\n",
    "from langchain_core.prompts import HumanMessagePromptTemplate\n",
    "# Instantiation using from_messages,input: BaseMessage/MessagePromptTemplate\n",
    "ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        SystemMessage(\n",
    "            content=(\n",
    "                \"You are a helpful AI bot. Your name is {name}.\"\n",
    "            )\n",
    "        ),\n",
    "        HumanMessagePromptTemplate.from_template(\"Hello, how are you doing?\"),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bc2f979c20881607",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T07:44:58.093562Z",
     "start_time": "2024-07-17T07:44:58.087621Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptTemplate(input_variables=[], messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=[], template='Hello, how are you doing?'))])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Instantiation using from_template, only human message\n",
    "ChatPromptTemplate.from_template(\"Hello, how are you doing?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d8d746e18aa6cf8",
   "metadata": {},
   "source": [
    "### Format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dd9d1fac6ac309d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content='You are a helpful AI bot. Your name is Bob.'),\n",
       " HumanMessage(content='Hello, how are you doing?'),\n",
       " AIMessage(content=\"I'm doing well, thanks!\"),\n",
       " HumanMessage(content='What is your name?')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call ChatPromptTemplate.format_messages()\n",
    "chat_template.format_messages(name=\"Bob\", user_input=\"What is your name?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f776c7e01db9f3de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"System: You are a helpful AI bot. Your name is Bob.\\nHuman: Hello, how are you doing?\\nAI: I'm doing well, thanks!\\nHuman: What is your name?\""
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call BaseChatPromptTemplate.format()\n",
    "chat_template.format(name=\"Bob\", user_input=\"What is your name?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3a6c8a09313a11ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content='You are a helpful AI bot. Your name is Bob.'), HumanMessage(content='Hello, how are you doing?'), AIMessage(content=\"I'm doing well, thanks!\"), HumanMessage(content='What is your name?')])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call BaseChatPromptTemplate.format_prompt()\n",
    "chat_template.format_prompt(name=\"Bob\", user_input=\"What is your name?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "26cab97e5ec521b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content='You are a helpful AI bot. Your name is Bob.'), HumanMessage(content='Hello, how are you doing?'), AIMessage(content=\"I'm doing well, thanks!\"), HumanMessage(content='What is your name?')])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# call BasePromptTemplate.invoke()\n",
    "chat_template.invoke({\"name\":\"Bob\", \"user_input\":\"What is your name?\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2915dd9873a1da3",
   "metadata": {},
   "source": [
    "## MessagePromptTemplate\n",
    "> LangChain provides different types of MessagePromptTemplate. The most commonly used are AIMessagePromptTemplate, SystemMessagePromptTemplate and HumanMessagePromptTemplate, which create an AI message, system message and human message respectively.\n",
    "> \n",
    "> LangChain提供不同类型的MessagePromptTemplate,最常用的是 AIMessagePromptTemplate 和 SystemMessagePromptTemplate HumanMessagePromptTemplate ，它们分别创建 AI 消息、系统消息和人类消息\n",
    "\n",
    "> BaseMessagePromptTemplate\n",
    "> <br>--> _StringImageMessagePromptTemplate \n",
    "> <br>&emsp;&emsp; --> SystemMessagePromptTemplate、AIMessagePromptTemplate、 HumanMessagePromptTemplate \n",
    "> <br>--> BaseStringMessagePromptTemplate \n",
    "> <br>&emsp;&emsp; --> ChatMessagePromptTemplate\n",
    "> <br>--> MessagesPlaceholder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e87609d9e04eebac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T08:01:16.445616Z",
     "start_time": "2024-07-17T08:01:16.437676Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatMessage(content='May the force be with you', role='Jedi')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import ChatMessagePromptTemplate,HumanMessagePromptTemplate\n",
    "\n",
    "prompt = \"May the {subject} be with you\"\n",
    "\n",
    "chat_message_prompt = ChatMessagePromptTemplate.from_template(\n",
    "    role=\"Jedi\", template=prompt\n",
    ")\n",
    "chat_message_prompt.format(subject=\"force\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "227b7bdde43b3897",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T08:16:51.956308Z",
     "start_time": "2024-07-17T08:16:51.950498Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='May the force be with you')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import AIMessagePromptTemplate\n",
    "\n",
    "prompt = \"May the {subject} be with you\"\n",
    "\n",
    "ai_message_prompt = AIMessagePromptTemplate.from_template(prompt)\n",
    "ai_message_prompt.format(subject=\"force\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "2d67c09d6279761c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T08:17:46.431897Z",
     "start_time": "2024-07-17T08:17:46.426882Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[AIMessage(content='May the force be with you')]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ai_message_prompt.format_messages(subject=\"force\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ada8951212804569",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "fc4888d221702ea5",
   "metadata": {},
   "source": [
    "## MessagesPlaceholder\n",
    "> LangChain also provides MessagesPlaceholder, which gives you full control of what messages to be rendered during formatting. This can be useful when you are uncertain of what role you should be using for your message prompt templates or when you wish to insert a list of messages during formatting.\n",
    "> \n",
    "> LangChain还提供 MessagesPlaceholder ，它使您可以完全控制在格式化过程中要呈现的消息。当您不确定应为消息提示模板使用什么角色时，或者当您希望在格式化过程中插入消息列表时，这可能很有用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bab2b19b4d668ff",
   "metadata": {},
   "source": [
    "#### Instantiation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "d8a7d1650555d290",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T08:49:08.533565Z",
     "start_time": "2024-07-17T08:49:08.527945Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptTemplate(input_variables=['word_count'], input_types={'conversation': typing.List[typing.Union[langchain_core.messages.ai.AIMessage, langchain_core.messages.human.HumanMessage, langchain_core.messages.chat.ChatMessage, langchain_core.messages.system.SystemMessage, langchain_core.messages.function.FunctionMessage, langchain_core.messages.tool.ToolMessage]]}, partial_variables={'conversation': []}, messages=[MessagesPlaceholder(variable_name='conversation', optional=True), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['word_count'], template='Summarize our conversation so far in {word_count} words.'))])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "# Instantiation using from_messages (recommended)\n",
    "chat_prompt=ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"placeholder\", \"{conversation}\"), \n",
    "        (\"human\", \"Summarize our conversation so far in {word_count} words.\"),\n",
    "    ]\n",
    ")\n",
    "chat_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a46ed271bd29ff49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptTemplate(input_variables=['conversation', 'word_count'], input_types={'conversation': typing.List[typing.Union[langchain_core.messages.ai.AIMessage, langchain_core.messages.human.HumanMessage, langchain_core.messages.chat.ChatMessage, langchain_core.messages.system.SystemMessage, langchain_core.messages.function.FunctionMessage, langchain_core.messages.tool.ToolMessage]]}, messages=[MessagesPlaceholder(variable_name='conversation'), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['word_count'], template='Summarize our conversation so far in {word_count} words.'))])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import (\n",
    "    ChatPromptTemplate,\n",
    "    HumanMessagePromptTemplate,\n",
    "    MessagesPlaceholder,\n",
    ")\n",
    "\n",
    "chat_prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        MessagesPlaceholder(variable_name=\"conversation\"), \n",
    "        HumanMessagePromptTemplate.from_template(\"Summarize our conversation so far in {word_count} words.\")\n",
    "    ]\n",
    ")\n",
    "chat_prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cc0b4f606e9c821",
   "metadata": {},
   "source": [
    "#### Format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5b54ff752802ee17",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T08:49:12.848388Z",
     "start_time": "2024-07-17T08:49:12.842069Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='What is the best way to learn programming?'),\n",
       " AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn.\\n\\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\\n\\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience'),\n",
       " HumanMessage(content='Summarize our conversation so far in 10 words.')]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import AIMessage, HumanMessage\n",
    "\n",
    "human_message = HumanMessage(content=\"What is the best way to learn programming?\")\n",
    "ai_message = AIMessage(\n",
    "    content=\"\"\"\\\n",
    "1. Choose a programming language: Decide on a programming language that you want to learn.\n",
    "\n",
    "2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\n",
    "\n",
    "3. Practice, practice, practice: The best way to learn programming is through hands-on experience\\\n",
    "\"\"\"\n",
    ")\n",
    "\n",
    "chat_prompt.format_messages(\n",
    "    conversation=[human_message, ai_message], word_count=\"10\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "cb8feec9687c1293",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T08:52:30.545562Z",
     "start_time": "2024-07-17T08:52:30.532919Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[HumanMessage(content='What is the best way to learn programming?'), AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn.\\n\\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\\n\\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience'), HumanMessage(content='Summarize our conversation so far in 10 words.')])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_prompt.invoke(\n",
    "    {\n",
    "        \"conversation\":[human_message, ai_message], \n",
    "        \"word_count\":\"10\"\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78b56e6795a9c992",
   "metadata": {},
   "source": [
    "## LCEL\n",
    "> PromptTemplate and ChatPromptTemplate implement the Runnable interface, the basic building block of the LangChain Expression Language (LCEL). This means they support invoke, ainvoke, stream, astream, batch, abatch, astream_log calls.<br>\n",
    "> PromptTemplate accepts a dictionary (of the prompt variables) and returns a StringPromptValue. A ChatPromptTemplate accepts a dictionary and returns a ChatPromptValue.<br>\n",
    "> \n",
    "> PromptTemplate和ChatPromptTemplate实现了Runnable接口，这是LangChain表达式语言（LCEL）的基本构建块。这意味着它们支持 invoke 、 ainvoke 、 stream 、 astream 、 batch、 abatch 、 astream_log 调用。<br>\n",
    "> PromptTemplate接受（提示变量的）字典并返回StringPromptValue.A ChatPromptTemplate 接受字典并返回ChatPromptValue"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a202225992f3f0b",
   "metadata": {},
   "source": [
    "### PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "be7d28290bd1c921",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T09:19:50.460607Z",
     "start_time": "2024-07-17T09:19:50.451091Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StringPromptValue(text='Tell me a funny joke about chickens.')"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "prompt_template = PromptTemplate.from_template(\n",
    "    \"Tell me a {adjective} joke about {content}.\"\n",
    ")\n",
    "\n",
    "prompt_val = prompt_template.invoke({\"adjective\": \"funny\", \"content\": \"chickens\"})\n",
    "prompt_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "6463e2e58d0333b0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T09:20:34.908064Z",
     "start_time": "2024-07-17T09:20:34.902682Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tell me a funny joke about chickens.'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_val.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "9f87a518a528f39a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T09:20:31.199157Z",
     "start_time": "2024-07-17T09:20:31.193826Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='Tell me a funny joke about chickens.')]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_val.to_messages()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfcff3cefc2b2e88",
   "metadata": {},
   "source": [
    "### ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e90d5f45075561ae",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T10:07:33.435099Z",
     "start_time": "2024-07-17T10:07:33.430179Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue(messages=[SystemMessage(content=\"You are a helpful assistant that re-writes the user's text to sound more upbeat.\"), HumanMessage(content='i dont like eating tasty things.')])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "chat_template = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\",\"You are a helpful assistant that re-writes the user's text to sound more upbeat.\"),\n",
    "        (\"human\",\"{text}\"),\n",
    "    ]\n",
    ")\n",
    "chat_val = chat_template.invoke({\"text\": \"i dont like eating tasty things.\"})\n",
    "chat_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e00dc175dc7a9ad",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T10:07:37.006703Z",
     "start_time": "2024-07-17T10:07:37.001785Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[SystemMessage(content=\"You are a helpful assistant that re-writes the user's text to sound more upbeat.\"),\n",
       " HumanMessage(content='i dont like eating tasty things.')]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_val.to_messages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "fe18f10e08f02a4e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-17T10:07:39.145741Z",
     "start_time": "2024-07-17T10:07:39.140771Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"System: You are a helpful assistant that re-writes the user's text to sound more upbeat.\\nHuman: i dont like eating tasty things.\""
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_val.to_string()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
